package com.wqz.dao;

import com.wqz.pojo.User;
import com.wqz.utils.MyBatisUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

public class UserDaoTest {

    @Test
    public void findUserById() {
        try {

            InputStream is = Resources.getResourceAsStream("mybatis.xml");
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            SqlSession sqlSession = factory.openSession(true);
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            System.out.println(userDao.findUserById(5));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Test
    public void get() {
        try {
            SqlSession sqlSession = MyBatisUtil.createSqlSession();
            UserDao userDao = sqlSession.getMapper(UserDao.class);
            List<User> userList = userDao.get(2,"孙");
            for(User user : userList) {
                System.out.println(user);
            }
            MyBatisUtil.close(sqlSession);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Test
    public void findByMap() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        Map<String, Object> map = new HashMap<>();
        map.put("sex",2);
        map.put("username","孙");
        try {
            List<User> userList = userDao.findByMap(map);
            for(User user : userList) {
                System.out.println(user);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close(sqlSession);
        }
    }

    @Test
    public void findByUser() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setGender(1);
        user.setUserName("张");
        try {
            List<User> userList = userDao.findByUser(user);
            for(User user1 : userList) {
                System.out.println(user1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close(sqlSession);
        }
    }

    @Test
    public void modifyUser() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setUserName("李明1212");
        user.setGender(1);
        user.setId(2);
        try {
            int result = userDao.modifyUser(user);
            System.out.println("被改变的行数：" + result);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close(sqlSession);
        }
    }

    @Test
    public void addUser() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setUserCode("wangwang");
        user.setUserName("旺旺");
        user.setUserPassword("123456");
        try{

            int result = userDao.addUser(user);
            System.out.println("添加成功，被改变的行数：" + result);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close(sqlSession);
        }
    }

    @Test
    public void deleteUser() {
        SqlSession sqlSession = MyBatisUtil.createSqlSession();
        UserDao userDao = sqlSession.getMapper(UserDao.class);
        User user = new User();
        user.setId(19);
        try {

            int result = userDao.deleteUser(user);

            System.out.println("删除成功，被删除的行数" + result);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MyBatisUtil.close(sqlSession);
        }
    }
}